/******************************************************************************
 * (C) Copyright 2015 Atech-Automotive
 * FILE NAME:    app_can_Itf.c
 * DESCRIPTION:
 * 
 * DATE BEGUN:   2015/08/18
 * BY:           martin.zhang
 * PRODUCT NAME:
 * APPLICATION:
 * TARGET H/W:
 * DOC REF:
 *****************************************************************************
 */

/*****************************************************************************
** #include 
*****************************************************************************/
#include <string.h>
#include "app_can_Itf.h"
#include "drv_can.h"
#include "msg_canApi.h"
#include "app_canNwm.h"
#include "msg_canList.h"
#include "uds_diag_drv.h"
#include "uds_can_trans.h"
#include "diag_fun.h"
#include "hwa_outputs.h"
#include "hwa_inputs.h"

//#define OSEK_NWM
/*****************************************************************************
**  CAN APP configuration
*****************************************************************************/
const UINT8 u8_canReadMsgsOnceMax   = CAN_RX_BUFFER_SIZE;
const UINT8 u8_canReadFastMsgsOnceMax   = CAN_RX_BUFFER_SIZE;
const UINT8 u8_canTxErrNumMax       = 8;
#ifdef OSEK_NWM
const BOOL   c_b_canOsekNwmEnable      = FALSE;//DY = TRUE;
const UINT32 c_u32_osekNmRxBase       = NWM_BASE_ADDR;
const UINT32 c_u32_osekNmRxIdMask     = NWM_RX_ALL_ID_MASK;
#else
const BOOL   c_b_canOsekNwmEnable      = FALSE;
const UINT32 c_u32_osekNmRxBase       = 0;
const UINT32 c_u32_osekNmRxIdMask     = 0;
#endif
/*****************************************************************************
** function prototypes
*****************************************************************************/

/****************************************************************************/
BOOL app_canItfGetIGN(void)
{
    //return hwa_inputs_u8_getInput(SI_PAD8_IN);
    return HWA_GetInput(1);
//    return TRUE;
}

BOOL app_canItfGetSTB(void)
{
    return  (PT0AD>>3)&0x01;//PT1AD &= 0x01; 

    //return hwa_outputs_u8_getOutputsStatus(SO_S_CAN_STB_OUT);
}

BOOL app_canItfGetCanBusOffState(void)
{
    return drv_can_e_getBusStatus();
}

void app_canItfEnableSTB(void)
{
//	hwa_vCanSTBOn();/* 0-low active */ 
//  PT1AD &= 0xFE;
	//PTS &= 0x7f;
	HWA_SetOutput(SO_PAD11_OUT,ON);

}
void app_canItfDisableSTB(void)
{
//	hwa_vCanSTBOff();/* 1-high inactive */ 
//   PT1AD |= 0x01;
	HWA_SetOutput(SO_PAD11_OUT,OFF);

}

BOOL app_canItfCanInit(void)
{
    if(drv_can_v_init() == CAN_SUCCESS)
    {
        app_canItfEnableSTB();
        (void)drv_can_e_wakeUp();
        return TRUE;
    }
    return FALSE;
}

BOOL app_canItfSendFrame(UINT32 u32_id, UINT8 u8_prio, UINT8 u8_length, UINT8 *p_u8_txData)
{
    if(drv_can_e_sendFrame(u32_id, u8_prio, u8_length, p_u8_txData) == CAN_SUCCESS)
    {
        return TRUE;
    }
    return FALSE;
}

void app_canItfGetMessage(UINT32 *u32_id, UINT8 *u8_length, UINT8 *p_u8_txData)
{
    DRV_CAN_MSG_S p_st_message;
    drv_can_v_getMessage(&p_st_message);

    *u32_id = p_st_message.u32_msgId;
    *u8_length = p_st_message.u8_dlc;
    (void)memcpy(p_u8_txData, p_st_message.u8_data, p_st_message.u8_dlc);
}

UINT8 app_canItfGetRxMessageCount(void)
{
    return drv_can_u8_getRxMessageCount();
}

void app_canItfUpdataTxMsgs(void)
{ 
 UINT8 temp=0;  
//   c_u16_wr(&CanMsg4E1_Tx, 38, 10, 0x1ff);
//   c_u8_wr(&CanMsg531_Tx, 0, 8, 0x30);
//   c_bool_wr(&CanMsg531_Tx,8,1,temp);
//  c_bool_wr(&CanMsg531_Tx,15,1,temp);
}

void app_canItfCanHandler10ms(void)
{
//  (void)drv_can_e_test();
//    app_canNwmHandler10ms();
}

void app_canItfCanMsgOsek_Rx(UINT32 u32_id, UINT8 u8_length, UINT8* p_u8_Data)
{
    DRV_CAN_MSG_S st_msg;

    st_msg.u32_msgId = u32_id;
    st_msg.u8_dlc = u8_length;
    (void)memcpy(st_msg.u8_data, p_u8_Data, u8_length);
#ifdef OSEK_NWM    
    receivecanNwmMsg(&st_msg);
    
#endif
}

void app_canItfRecMsg(UINT32 u32_id, UINT8 u8_length, UINT8* p_u8_Data)
{
    (void)u8_length;
    
    switch(u32_id)
    {
        case 0x774://0x7FA://UDS physical request
            {
                CanTrans_Rx(p_u8_Data, CanMsg774_Rx.Length, DIAG_PHYSIC_ADDR_MODE);
            }
        break;
                
        case 0x7DF://UDS functional request
            {
                CanTrans_Rx(p_u8_Data, CanMsg7DF_Rx.Length, DIAG_FUNCTION_ADDR_MODE);
            }
        break;      
    }  
}


void app_canItfCanWakeUp(void)
{
    (void)drv_can_e_wakeUp();
}

BOOL app_canItfCanSleep(void)
{
    if(drv_can_e_sleep() == CAN_SUCCESS)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
BOOL app_canItfGetSleepSts(void)
{
    return drv_can_b_getSleepSts();
}


#define CAN_FAST_DEAL
/*
 * just for CAN GW.
 * Other module,please keep the default form 
*/
#if( CAN_RX_FAST == TRUE )
void app_canItfCanHandlerRt(void)
{
    
}

void app_canItfRecFastMsg(UINT32 u32_id, UINT8 u8_length, UINT8* p_u8_Data)
{

}

void app_canItfGetFastMessage(UINT32 *u32_id, UINT8 *u8_length, UINT8 *p_u8_txData)
{
    DRV_CAN_MSG_S p_st_message;
    drv_can_v_getFastMessage(&p_st_message);

    *u32_id = p_st_message.u32_msgId;
    *u8_length = p_st_message.u8_dlc;
    (void)memcpy(p_u8_txData, p_st_message.u8_data, p_st_message.u8_dlc);
}

UINT8 app_canItfGetFastRxMessageCount(void)
{
    return drv_can_u8_getRxFastMessageCount();
}
#endif

/*for other interface*/
/*****************************************************************************/
/**
* Function Name: getAirbagStatus
* Description:  convert CAN airbag crash data 
*
* Param:	 none
* Return:  rpm
* Author:  AF
* *****************************************************************************/
  
/*****************************************************************************
** End File
*****************************************************************************/
